package com.xianbeiying.app.ckgl.data;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

//	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static String getUtcTime(String time) {
		try {
			Date date = format.parse(time);
			return String.valueOf(date.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date strToDateLong(String strDate) {

		ParsePosition pos = new ParsePosition(0);
		Date strtodate = format.parse(strDate, pos);
		return strtodate;
	}
	
	public static String convert2String(long time) {
		if (time > 0l) {

			Date date = new Date(time);
			return format.format(date);
		}
		return "";
	}
	
	public static String vonvertTimeForDevice(String time)
	{
		StringBuffer result = new StringBuffer();
		if (time.length() != 20)
		{
			Date date = new Date();
			time = format.format(date);
		}
		result.append(time.subSequence(2, 4));
		result.append(time.subSequence(5, 7));
		result.append(time.subSequence(8, 10));
		result.append(time.subSequence(12, 14));
		result.append(time.subSequence(15, 17));
		result.append(time.subSequence(18, 20));
		return result.toString();
	}
	public static long convertTimeFromDevice(String date)
	{
		StringBuffer sb = new StringBuffer();
		if (date.length() != 12)
		{
			return System.currentTimeMillis();
		}
		else
		{
			sb.append("20");
			sb.append(date.substring(0, 2));
			sb.append("年");
			sb.append(date.substring(2, 4));
			sb.append("月");
			sb.append(date.substring(4, 6));
			sb.append("日 ");
			sb.append(date.substring(6, 8));
			sb.append(":");
			sb.append(date.substring(8, 10));
			sb.append(":");
			sb.append(date.substring(10, 12));

		}
		Date time = new Date();
		try {
			time = format.parse(sb.toString());
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time.getTime();
	}
	
	public static byte[] getYMDHMFromDate(Date date) {
		byte[] bytes = new byte[5];
		if (date != null) {
			Calendar cld = Calendar.getInstance();
			cld.setTime(date);
			int year = cld.get(Calendar.YEAR);
			bytes[0] = (byte) Integer.valueOf(String.valueOf(year).substring(2)).intValue();
			bytes[1] = (byte) (cld.get(Calendar.MONTH) + 1);
			bytes[2] = (byte) cld.get(Calendar.DAY_OF_MONTH);
			bytes[3] = (byte) cld.get(Calendar.HOUR_OF_DAY);
			bytes[4] = (byte) cld.get(Calendar.MINUTE);
		}

		return bytes;
	}
	
	public static byte[] getYMDHMSFromDate(Date date) {
		byte[] bytes = new byte[6];
		if (date != null) {
			Calendar cld = Calendar.getInstance();
			cld.setTime(date);
			int year = cld.get(Calendar.YEAR);
			bytes[0] = (byte) Integer.valueOf(String.valueOf(year).substring(2)).intValue();
			bytes[1] = (byte) (cld.get(Calendar.MONTH) + 1);
			bytes[2] = (byte) cld.get(Calendar.DAY_OF_MONTH);
			bytes[3] = (byte) cld.get(Calendar.HOUR_OF_DAY);
			bytes[4] = (byte) cld.get(Calendar.MINUTE);
			bytes[5] = (byte) cld.get(Calendar.SECOND);
		}

		return bytes;
	}
	
	public static String getYMDHMFromByte(byte[] bytes)
	{
		return String.format("20%02d-%02d-%02d %02d:%02d", bytes[0],bytes[1],bytes[2],bytes[3],bytes[4]);
	}
	
	public static String getStrYMDHMFromDate(Date dateDate) {
		if (dateDate != null) {
			return format.format(dateDate);
		} else {
			return "";
		}
	}
	
	public static Date getDateFromByte(byte[] bytes)
	{
		Calendar cld = Calendar.getInstance();
		cld.set(2000 + bytes[0], bytes[1]-1, bytes[2], bytes[3], bytes[4]);
		return cld.getTime();
	}
	
	public static Date getDateTimeFromByte(byte[] bytes)
	{
		Calendar cld = Calendar.getInstance();
		cld.set(2000 + bytes[0], bytes[1]-1, bytes[2], bytes[3], bytes[4], bytes[5]);
		return cld.getTime();
	}
	
}
